
import { _decorator, Component, Node, SpriteFrame, ScrollView, Prefab, game } from 'cc';
import GlobalPool from '../../Script/Common/GlobalPool';
import Loader from '../../Script/Common/Loader';
import PlayerData from '../../Script/Common/PlayerData';
import { PlayerDataType } from '../../Script/GameSpecial/PlayerDataTemplate';
import { ShopDataCfg, ShopDataType } from '../../Script/GameSpecial/shopDataCfg';
import { ShopDisplay } from './shopDisplay';
import { ShopItem } from './shopItem';
const { ccclass, property } = _decorator;

@ccclass('ShopViews')
export class ShopViews extends Component {
    @property(Prefab)
    shopItemPerfab: Prefab = null;

    @property(ShopDisplay)
    shopDisplay: ShopDisplay = null;

    //存储所有商品图片
    _spritArr: { [type: number]: { [id: number]: SpriteFrame } } = {};
    //根据类型 记录商品页面
    _views: { [type: number]: viewCmp } = {};

    onLoad() {
        //初始化
        this._views = {};
        for (let i = 0; i < this.node.children.length; i++) {
            const e = this.node.children[i];
            let type = i + 1;
            this._views[type] = new viewCmp(type, e, this);
        }
        //创建对象池        
        GlobalPool.createPool('shopItem', this.shopItemPerfab);

    }

    //切换商品页面的内容
    changView(type: number) {
        this.loadSprites(type, () => {
            this.showView(type);
        });
    }

    //显示指定的页面数据
    showView(type: number) {
        //
        let data = ShopDataCfg[type];
        if (!data) {
            console.log('没有配置商城数据:' + type);
            return
        }
        //切换页面
        for (const key in this._views) {
            let isShow = type == this._views[key]._type;
            this._views[key].setShow(isShow);
        }
        //当前页面填充数据
        this._views[type].setData(data, this._spritArr[type]);

        //切换展示
        this.shopDisplay.display(type, this._views[type]._curId);
    }

    //加载对应商品类型的图标
    loadSprites(type: number, cb?) {
        if (this._spritArr[type]) {
            cb && cb();
            return;
        }
        // this._spritArr[type] = {};

        let url = 'Image/Shop/goods';
        Loader.loadBundle("UI", () => {
            Loader.loadBundleDir("UI", url, (res: SpriteFrame[]) => {
                for (let i = 0; i < res.length; i++) {
                    const s = res[i];
                    let type1 = parseInt(s.name[3]);
                    let id = parseInt(s.name[5]);
                    if (!this._spritArr[type1]) {
                        this._spritArr[type1] = {};
                    }
                    this._spritArr[type1][id] = s;
                }
                cb && cb();
            }, SpriteFrame, false);
        })
    }
}

export class viewCmp {
    _type = 0;
    _node: Node = null;
    content: Node = null;
    scrollview: ScrollView = null;

    _curId = 0;
    items: { [id: number]: ShopItem } = {};
    _shopViews: ShopViews = null;

    constructor(_type: number, _node: Node, shopViews: ShopViews) {
        this._type = _type;
        this._node = _node;
        this._shopViews = shopViews;
        this.scrollview = this._node.getComponent(ScrollView);
        this.content = this.scrollview.content;
        this.content.removeAllChildren();
    }
    setShow(isShow) {
        this._node.active = isShow;
    }
    //填充页面数据
    setData(data: ShopDataType[], sfRes: { [id: number]: SpriteFrame }) {
        //玩家存档
        let gameData = PlayerData.getData('gameData') as PlayerDataType;

        let isLock = true;
        let isUse = false;

        for (const key in data) {
            const d: ShopDataType = data[key];
            if (d.id !== 0) {
                //是否解锁
                if (gameData.shopData[d.type] &&
                    gameData.shopData[d.type][d.id]) {
                    isLock = false;
                } else {
                    isLock = true;
                }
                //是否使用
                isUse = gameData.useSkin[d.type] == d.id;
                if (isUse) {
                    this._curId = d.id;
                }
                this.setShopItem(sfRes[d.id], d, isLock, isUse);
            }
        }
    }
    //创建商品item+数据
    setShopItem(sf: SpriteFrame, d: ShopDataType, isLock: boolean, isUse: boolean) {
        let inData = {
            data: d, sf: sf, isLock: isLock, isUse: isUse,
            useChangeCb: this.onUsedChange.bind(this), displayCb: this.onDisplayChange.bind(this)
        };

        let cmp = this.items[d.id];
        if (!cmp) {
            let item = GlobalPool.get('shopItem');
            item.parent = this.content;
            cmp = item.getComponent(ShopItem);
            this.items[d.id] = cmp;
        }
        //设置数据
        cmp.setData(inData);
    }
    //当前类型的商品使用更换时调用
    onUsedChange(id: number) {
        for (const key in this.items) {
            this.items[key].setUseState(id);
        }
        this._curId = id;

        this.onDisplayChange(id);
    }
    //当前类型的商品展示更换时调用
    onDisplayChange(id: number) {
        this._shopViews.shopDisplay.display(this._type, id);
    }

}